package tmi.li.txclck;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;
import android.os.Build.VERSION;
import android.widget.RemoteViews;


import tmi.li.txclck.R;


public class TxClckProvider extends AppWidgetProvider {
	//private static String TAG = "tmi.li.txclck.TxClckProvider";
	public static int _hex = -1;

	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		context.startService(new Intent(context, Time.class));
		final int N = appWidgetIds.length;
		for (int i=0; i<N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateWidget(context, appWidgetManager, appWidgetId);
		}
	}
	
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        final int N = appWidgetIds.length;
        TxPref txpref = new TxPref(TxPref.PREFS_NAME_TXCLCK);
        for (int i=0; i<N; i++) {
            txpref.deletePreAndId(context, appWidgetIds[i]);
        }
    }


		
    static public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    	context.stopService(new Intent(context, Time.class));
    	//getting the Preferences of the widget
    	TxPref txpref = new TxPref(TxPref.PREFS_NAME_TXCLCK);
    	String s = txpref.getPrefs(context, Integer.valueOf(appWidgetId));
    	
    	// if s == null the Configuration Dialog hasnt ended yet, so do nothin
    	if(s != null) {
    		String[] prefs = s.split(";");
    		String bhex = prefs[0];
    		String hex = prefs[1];
    		String format = prefs[2];
	
	
		    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
		    TxClckTime tm = new TxClckTime();
		    
		    //setting the Time
		    views.setTextViewText(R.id.hours, (format.contains("4")) ? tm.getHours(1) : tm.getHours(0) );
		    
		    //setting the descriptions
		    views.setTextViewText(R.id.am, tm.getDesc());
		    
		    //setting the minutes 
		    views.setTextViewText(R.id.minutes, tm.getMinutes());
		    
		    //setting the Text Color
		    views.setTextColor(R.id.minutes, Color.parseColor(hex));
		    views.setTextColor(R.id.am, Color.parseColor(hex));
		    views.setTextColor(R.id.hours, Color.parseColor(hex));
		    
		    //setting the Background Color, only for android 2.2 aka froyo
		    if(VERSION.SDK_INT >= 8) {
	        	views.setInt(R.id.linlay, "setBackgroundColor", Color.parseColor(bhex));
	        }
		    
		    
		    //setting the pending intent
		    if(txpref.getAppName(context, appWidgetId) != null) {
	           	String appname = txpref.getAppName(context, appWidgetId);
	           	Intent intent = new Intent(Intent.ACTION_MAIN);
	        	intent.setComponent(ComponentName.unflattenFromString(appname));
	        	PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	        	views.setOnClickPendingIntent(R.id.linlay, pendingIntent);
	        }
		    
		    // Tell the AppWidgetManager to perform an update on the current App Widget
		    appWidgetManager.updateAppWidget(appWidgetId, views);
    	}
    }

    
    public static class Time extends Service {
		
	    @Override
	    public void onStart(Intent intent, int startId) {
	    	
	    	//registering TxClckReceiver for various Broadcasts
			IntentFilter it = new IntentFilter(Intent.ACTION_TIME_TICK);
			BroadcastReceiver mReceiver = new TxClckReceiver();
			this.registerReceiver(mReceiver, it);
			it = new IntentFilter(Intent.ACTION_SCREEN_ON);
			this.registerReceiver(mReceiver, it);

	    }

	    @Override
	    public IBinder onBind(Intent intent) {
	        return null;
	    }   
	}			
}